package com.jtpay.sdk.app;

import android.app.Activity;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.support.multidex.MultiDex;

import com.channel.test.DaoMaster;
import com.channel.test.DaoSession;
import com.jtpay.sdk.BuildConfig;
import com.jtpay.sdk.activity.AppPackage;
import com.jtpay.sdk.jtpay.utils.PayLogUtil;
import com.jtpay.sdk.jtpay.PayUtil;
import com.jtpay.sdk.util.DbUtils;
import com.tencent.bugly.Bugly;
import com.tencent.bugly.beta.Beta;
import com.tencent.tinker.loader.app.TinkerApplication;
import com.tencent.tinker.loader.shareutil.ShareConstants;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Steven on 2016/6/21 0021.
 * update by Joe
 * add greenDao DataBase operation
 */

public class JftApplication extends TinkerApplication {
    private static final String APP_ID = "4e34dd7d4e";
    public SQLiteDatabase db;
    public DaoSession daoSession;
    private PayUtil mPayUtil;
    private List<Activity> mActivities;

    public JftApplication() {
        super(ShareConstants.TINKER_ENABLE_ALL, "com.jtpay.sdk.bugly.BuglyApplicationLike",
                "com.tencent.tinker.loader.TinkerLoader", false);
        mActivities = new ArrayList<>();
        mPayUtil = new PayUtil();
    }

    public static JftApplication getInstance() {
        return Holder.mInstance;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Bugly.init(this, APP_ID, false);
        logTest();
        setupDataBase();
        /*CrashHandler handler = CrashHandler.getInstance();
        handler.init(getApplicationContext());*/
    }

    private void logTest() {
        AppPackage.getInstance().init(getApplicationContext());
        AppPackage appPackage = AppPackage.getInstance();
        String packageName = appPackage.getPackageName();
        String versionCode = appPackage.getVersionCode();
        String versionName = appPackage.getVersionName();
        String packName = appPackage.getPackName();
        PayLogUtil.log(" packageName = " + packageName + " versionCode = " + versionCode + " versionName = " + versionName + " packName = " + packName);
    }

    public PayUtil getPayUtil() {
        return mPayUtil;
    }

    public void addActivity(Activity activity) {
        mActivities.add(activity);
    }

    public void exit() {
        for (Activity activity : mActivities) {
            activity.finish();
        }
        System.exit(0);
    }

    private void setupDataBase() {
        // 通过 DaoMaster 的内部类 DevOpenHelper，你可以得到一个便利的 SQLiteOpenHelper 对象。
        // 可能你已经注意到了，你并不需要去编写「CREATE TABLE」这样的 SQL 语句，因为 greenDAO 已经帮你做了。
        // 注意：默认的 DaoMaster.DevOpenHelper 会在数据库升级时，删除所有的表，意味着这将导致数据的丢失。
        // 所以，在正式的项目中，你还应该做一层封装，来实现数据库的安全升级。
        //创建数据库
        DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, BuildConfig.dbName, null);
        //得到数据库连接对象
        db = helper.getWritableDatabase();
        // 注意：该数据库连接属于 DaoMaster，所以多个 Session 指的是相同的数据库连接。
        DaoMaster daoMaster = new DaoMaster(db);
        //得到daoSession对象
        daoSession = daoMaster.newSession();
        DbUtils.createDir(DbUtils.SDPATHHome);
        DbUtils.createDir(DbUtils.SDPath);
    }

    public SQLiteDatabase getSQLiteDatabase() {
        return db;
    }

    public DaoSession getDaoSession() {
        return daoSession;
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        // you must install multiDex whatever tinker is installed!
        MultiDex.install(base);


        // 安装tinker
        Beta.installTinker();
    }

    private static class Holder {
        static JftApplication mInstance = new JftApplication();
    }
}
